/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.app.fragment.CompareHist1Fragment;
import com.cv4j.app.fragment.CompareHist2Fragment;
import com.cv4j.app.fragment.CompareHist3Fragment;
import com.cv4j.app.provider.CompareHistProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * 直方图比较
 *
 * @since 2021-05-19
 */
public class CompareHistAbilitySlice extends BaseAbilitySlice
    implements TabList.TabSelectedListener, PageSlider.PageChangedListener {
    private TabList tabList;
    private PageSlider pageSlider;
    private List<Component> mList = new ArrayList<>();
    private String[] titleList = new String[]{"原图和直方图均衡化", "两张相同的图", "两张完全不同的图"};

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_compare_hist;
    }

    @Override
    protected void initComponents(Intent intent) {
        mList.add(CompareHist1Fragment.getInstance(getContext()).getComponent());
        mList.add(CompareHist2Fragment.getInstance(getContext()).getComponent());
        mList.add(CompareHist3Fragment.getInstance(getContext()).getComponent());
        tabList = (TabList) findComponentById(ResourceTable.Id_tabList);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pagerSlider);
        pageSlider.setSlidingPossible(true);
        tabList.addTabSelectedListener(this);
        pageSlider.addPageChangedListener(this);
        for (String title : titleList) {
            TabList.Tab tab = tabList.new Tab(getContext());
            tab.setText(title);
            tabList.addTab(tab);
        }
        tabList.selectTabAt(0);

        pageSlider.setProvider(new CompareHistProvider(mList));
        pageSlider.setCurrentPage(0);
        pageSlider.setReboundEffect(false);
    }

    @Override
    public void onPageSliding(int i1, float v1, int i2) {
    }

    @Override
    public void onPageSlideStateChanged(int i1) {
    }

    @Override
    public void onPageChosen(int i1) {
        if (tabList.getSelectedTab().getPosition() != i1) {
            tabList.selectTab(tabList.getTabAt(i1));
        }
    }

    @Override
    public void onSelected(TabList.Tab tab) {
        if (pageSlider.getCurrentPage() != tab.getPosition()) {
            pageSlider.setCurrentPage(tab.getPosition());
        }
    }

    @Override
    public void onUnselected(TabList.Tab tab) {
    }

    @Override
    public void onReselected(TabList.Tab tab) {
    }
}
